﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using Urb.Weather.Core;
using System.Windows.Media.Imaging;
using System.Globalization;

namespace Urb.Weather.Ticker.UI.Converters
{
    [ValueConversion(typeof(bool), typeof(ImageSource))]
    public abstract class AtmosphereConditionsCodeToImageSourceConverter : IValueConverter
    {
        public abstract string ImageSize { get; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;

            if (!(value is AtmosphereConditionsCode?) && !(value is AtmosphereConditionsCode))
                return null;

            AtmosphereConditionsCode code;

            if (value is AtmosphereConditionsCode?)
            {
                var concreteValue = (AtmosphereConditionsCode?)value;

                if (!concreteValue.HasValue)
                    return null;

                code = concreteValue.Value;
            }
            else
            {
                code = (AtmosphereConditionsCode)value;
            }

            string imageFileName = null;

            switch (code)
            {
                case AtmosphereConditionsCode.FairDay:
                    imageFileName = "FairDay.png";
                    break;
                case AtmosphereConditionsCode.FairNight:
                    imageFileName = "FairNight.png";
                    break;
                case AtmosphereConditionsCode.FewCloudsDay:
                    imageFileName = "FewCloudsDay.png";
                    break;
                case AtmosphereConditionsCode.FewCloudsNight:
                    imageFileName = "FewCloudsNight.png";
                    break;
                case AtmosphereConditionsCode.Overcast:
                    imageFileName = "Overcast.png";
                    break;
                case AtmosphereConditionsCode.Rain:
                    imageFileName = "Rain.png";
                    break;
                case AtmosphereConditionsCode.Snow:
                    imageFileName = "Snow.png";
                    break;
                case AtmosphereConditionsCode.Thunderstorm:
                    imageFileName = "Thunderstorm.png";
                    break;
                case AtmosphereConditionsCode.Fog:
                    imageFileName = "Fog.png";
                    break;
                case AtmosphereConditionsCode.Other:
                    return null;
            }

            if (imageFileName == null)
                return null;

            imageFileName = string.Format("{0}/{1}/{2}", "Resources/WeatherIcons", ImageSize, imageFileName);

            var imageSourceUri = new Uri(imageFileName, UriKind.Relative);

            BitmapImage image = new BitmapImage(imageSourceUri);

            return image;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class AtmosphereConditionsCodeToImageSourceConverter16px : AtmosphereConditionsCodeToImageSourceConverter
    {
        public override string ImageSize
        {
            get { return "16"; }
        }
    }

    public class AtmosphereConditionsCodeToImageSourceConverter32px : AtmosphereConditionsCodeToImageSourceConverter
    {
        public override string ImageSize
        {
            get { return "32"; }
        }
    }
}
